package com.mmlc.meimei.lichengkongjian.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.mmlc.businessutils.PullToRefreshUtils;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.been.CustomShowListView_Order1Bean;

import java.util.List;

/**
 * Created by Administrator on 2016/8/30 0030.
 */
public class CustomShowListView extends FrameLayout implements ViewTreeObserver.OnPreDrawListener {

    int showType;
    PullToRefreshListView listView;
    public CustomShowListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        getViewTreeObserver().addOnPreDrawListener(this);
        View view = View.inflate(getContext(), R.layout.view_listview,this);
        listView = (PullToRefreshListView) view.findViewById(R.id.refreshList);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomShowListView);
        showType = a.getInt(R.styleable.CustomShowListView_styleType,0);
        a.recycle();
    }

    PullToRefreshUtils pullUtils;
    private void processOrder1() {

        if(config == null){
            return;
        }
        final List<CustomShowListView_Order1Bean> bean = (List<CustomShowListView_Order1Bean>) config.getAdapterList();

        if(bean == null || bean.size() ==0){
            return;
        }
        MyOrder1Adapter order1Adapter = new MyOrder1Adapter(bean);
        listView.setAdapter(order1Adapter);
    }

    private void initOrder1(View view, CustomShowListView_Order1Bean bean) {
        TextView tv_order_number = (TextView) view.findViewById(R.id.tv_order_number);
        tv_order_number.setText(bean.orderNumber);

        TextView tv_orderState = (TextView) view.findViewById(R.id.tv_orderState);
        tv_orderState.setText(bean.orderState);

        TextView tv_allShopPrice = (TextView) view.findViewById(R.id.tv_allShopPrice);
        tv_allShopPrice.setText("产品总价: ¥"+bean.allShopPrice);

        MuchOrderItemGroup mg_itemGroup = (MuchOrderItemGroup) view.findViewById(R.id.mg_itemGroup);
        mg_itemGroup.addOrder1(bean.itemBeenList);

        MuchButtonGroup mg_buttonGroup = (MuchButtonGroup) view.findViewById(R.id.mg_buttonGroup);
        mg_buttonGroup.addButton(bean.orderButtonType,config);
    }


    @Override
    public boolean onPreDraw() {
        getViewTreeObserver().removeOnPreDrawListener(this);
        switch (showType){
            case 0:
                processOrder1();
                break;
            case 1:
                break;
        }

        return false;
    }

    public interface ViewConfig{
        public Object getAdapterList();
        public void buttonClicked(View v, int tag);
        public void itemClicked(View v);
    }
    ViewConfig config;
    public  void setViewConfig(ViewConfig config){
       this.config = config;
    }

    public class MyOrder1Adapter extends BaseAdapter{

        List<CustomShowListView_Order1Bean> bean;
        public MyOrder1Adapter(List<CustomShowListView_Order1Bean> bean){
            this.bean = bean;
        }
        @Override
        public int getCount() {
            return bean.size();
        }

        @Override
        public Object getItem(int position) {
            return bean.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(getContext(),R.layout.view_order1,null);
            initOrder1(view,bean.get(position));
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    config.itemClicked(v);
                }
            });
            return view;
        }
    }
}
